package org.linlinjava.litemall.db.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.linlinjava.litemall.db.handler.IntegerArrayJsonTypeHandler;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * litemall管理
 *
 * @author yliyun
 * @date 2022/11/23
 */
@Data
@TableName("litemall_admin")
public class LitemallAdmin {
    /**
     * 被删除
     */
    public static final Boolean IS_DELETED = Deleted.IS_DELETED.value();

    /**
     * 不删除
     */
    public static final Boolean NOT_DELETED = Deleted.NOT_DELETED.value();

    /**
     * id
     */
    @TableId
    private Integer id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 最后一次登录知识产权
     */
    private String lastLoginIp;

    /**
     * 最后登录时间
     */
    private LocalDateTime lastLoginTime;

    /**
     * 《阿凡达》
     */
    private String avatar;

    /**
     * 添加时间
     */
    private LocalDateTime addTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 删除
     */
    @TableLogic
    private Boolean deleted;

    /**
     * 角色id
     */
    @TableField(typeHandler = IntegerArrayJsonTypeHandler.class)
    private Integer[] roleIds;

    /**
     * 尼克名字
     */
    private String nickName;

    /**
     * 位置
     */
    private String location;

    /**
     * 移动
     */
    private String mobile;

    /**
     * 商店id
     */
    private Integer shopId;

    /**
     * 电子邮件
     */
    private String email;

    /**
     * 代码
     */
    private String code;

    /**
     * 社会安全号码
     */
    private String socialSecurityNumber;

    /**
     * 性别
     */
    private Integer gender;

    /**
     * 生日
     */
    private LocalDate birthday;

    /**
     * 位置国家
     */
    private Integer locationCountry;

    /**
     * 位置省份
     */
    private Integer locationProvince;

    /**
     * 位置城市
     */
    private Integer locationCity;

    /**
     * 邮政编码
     */
    private String postalCode;

    /**
     * 备注信息
     */
    private String remark;

    public enum Deleted {
        NOT_DELETED(new Boolean("0"), "未删除"),
        IS_DELETED(new Boolean("1"), "已删除");

        private final Boolean value;

        private final String name;

        Deleted(Boolean value, String name) {
            this.value = value;
            this.name = name;
        }

        public Boolean getValue() {
            return this.value;
        }

        public Boolean value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }
    }

    public enum Column {
        id("id", "id", "INTEGER", false),
        username("username", "username", "VARCHAR", false),
        password("password", "password", "VARCHAR", true),
        lastLoginIp("last_login_ip", "lastLoginIp", "VARCHAR", false),
        lastLoginTime("last_login_time", "lastLoginTime", "TIMESTAMP", false),
        avatar("avatar", "avatar", "VARCHAR", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false),
        roleIds("role_ids", "roleIds", "VARCHAR", false),
        nickName("nick_name", "nickName", "VARCHAR", false),
        location("location", "location", "VARCHAR", true),
        mobile("mobile", "mobile", "VARCHAR", false),
        shopId("shop_id", "shopId", "INTEGER", false),
        email("email", "email", "VARCHAR", false),
        code("code", "code", "VARCHAR", false),
        socialSecurityNumber("social_security_number", "socialSecurityNumber", "VARCHAR", false),
        gender("gender", "gender", "INTEGER", false),
        birthday("birthday", "birthday", "DATE", false),
        locationCountry("location_country", "locationCountry", "INTEGER", false),
        locationProvince("location_province", "locationProvince", "INTEGER", false),
        locationCity("location_city", "locationCity", "INTEGER", false),
        postalCode("postal_code", "postalCode", "VARCHAR", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
